import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, CanLoad, Route, RouterStateSnapshot, UrlTree} from '@angular/router';
import {Observable} from 'rxjs';
import {AuthService} from "../service/auth.service";

@Injectable({
  providedIn: 'root'
})
export class GuestGuard implements CanActivate, CanLoad {

  //TODO: 路由守卫的实现有问题，暂时放在这里不管了
  constructor(
    private authSrv: AuthService
  ) {
  }

  check(): Observable<boolean> {
    return new Observable((observer) => {
      if (this.authSrv.isLogin) {
        observer.next(true);
        observer.complete();
        console.debug(this.authSrv.isLogin)
        return;
      }
      observer.next(false);
      observer.complete();
    });
  }

  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    return true;
  }

  canLoad(route: Route): boolean | Observable<boolean> | Promise<boolean> {
    return this.check();
  }

}
